package org.dromara.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.dto.WbItemVisitLogsDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbItemVisitLogsVo;
import org.dromara.system.domain.bo.WbItemVisitLogsBo;
import org.dromara.system.service.IWbItemVisitLogsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 项目浏览记录
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/itemVisitLogs")
public class WbItemVisitLogsController extends BaseController {

    private final IWbItemVisitLogsService wbItemVisitLogsService;

    /**
     * 查询项目浏览记录列表
     */
    @SaCheckPermission("system:itemVisitLogs:list")
    @GetMapping("/list")
    public Map<String, WbItemVisitLogsDTO> list(Long itemId, PageQuery pageQuery) {
        List<WbItemVisitLogsDTO> wbItemVisitLogsDTOS = wbItemVisitLogsService.queryGroupTime(itemId, pageQuery);
        // 将Date转换为String的格式化对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // 将List<WbItemVisitLogsDTO>转换为Map<String, WbItemVisitLogsDTO>
        Map<String, WbItemVisitLogsDTO> collect = wbItemVisitLogsDTOS.stream()
            .collect(Collectors.toMap(
                wbItemVisitLogsDTO -> dateFormat.format(wbItemVisitLogsDTO.getGroupTime()), // 将Date转换为String
                wbItemVisitLogsDTO -> wbItemVisitLogsDTO
            ));

//        Map<Date, WbItemVisitLogsDTO> collect = wbItemVisitLogsDTOS.stream().collect(Collectors.toMap(WbItemVisitLogsDTO::getGroupTime, wbItemVisitLogsDTO -> wbItemVisitLogsDTO));
        return collect;
    }

    /**
     * 导出项目浏览记录列表
     */
    @SaCheckPermission("system:itemVisitLogs:export")
    @Log(title = "项目浏览记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbItemVisitLogsBo bo, HttpServletResponse response) {
        List<WbItemVisitLogsVo> list = wbItemVisitLogsService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目浏览记录", WbItemVisitLogsVo.class, response);
    }

    /**
     * 获取项目浏览记录详细信息
     * @param id 主键
     */
    @SaCheckPermission("system:itemVisitLogs:query")
    @GetMapping("/{id}")
    public R<WbItemVisitLogsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbItemVisitLogsService.queryById(id));
    }

    /**
     * 新增项目浏览记录
     */
    @SaCheckPermission("system:itemVisitLogs:add")
    @Log(title = "项目浏览记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbItemVisitLogsBo bo) {
        /*
        新增逻辑
        先判断浏览记录今天是否存在
         */
        if(wbItemVisitLogsService.queryTime(bo) != null){
            wbItemVisitLogsService.updateByBo(bo);
            //存在就修改浏览时间
            return R.ok();
        }
        return toAjax(wbItemVisitLogsService.insertByBo(bo));
    }

    /**
     * 修改项目浏览记录
     */
    @SaCheckPermission("system:itemVisitLogs:edit")
    @Log(title = "项目浏览记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbItemVisitLogsBo bo) {
        return toAjax(wbItemVisitLogsService.updateByBo(bo));
    }

    /**
     * 删除项目浏览记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:itemVisitLogs:remove")
    @Log(title = "项目浏览记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbItemVisitLogsService.deleteWithValidByIds(List.of(ids), true));
    }
}
